<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="98px">
      <el-row type="flex">
        <el-col :span="10">
          <el-form-item label="样本框编码" prop="sampleboxCode">
            <el-input v-model="queryParams.sampleboxCode" placeholder="请输入样本框编码" clearable
              @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="名称" prop="sampleboxName">
            <el-input v-model="queryParams.sampleboxName" placeholder="请输入名称" clearable
              @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="样本框类型" prop="sampleboxType">
            <el-select v-model="queryParams.sampleboxType" placeholder="请选择样本框类型" clearable>
              <el-option v-for="dict in dict.type.erecord_samplebox_type" :key="dict.value" :label="dict.label"
                :value="dict.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="10"></el-col>
      </el-row>
      <el-row type="flex">
        <el-col :span="10">
          <el-form-item label="所属区域" prop="region">
            <treeselect v-model="queryParams.region" :options="regionOptions" :show-count="true" placeholder="请选择所属区域"
              style="width: 230px" />
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="所属街道" prop="street">
            <treeselect v-model="queryParams.street" :options="regionOptions" :show-count="true" placeholder="请选择所属街道"
              style="width: 230px" />
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="所属社区" prop="community">
            <treeselect v-model="queryParams.community" :options="regionOptions" :show-count="true" placeholder="请选择所属社区"
              style="width: 230px" />
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['erecord:samplebox:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['erecord:samplebox:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['erecord:samplebox:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['erecord:samplebox:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sampleboxList" @selection-change="handleSelectionChange"
      @row-click="handleDetail" >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column align="center" label="序号" width="55">
        <template slot-scope="scope">
          <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>      
      <el-table-column label="id" align="center" prop="id" v-if="false" />
      <el-table-column label="样本框编码" align="center" prop="sampleboxCode" v-if="true" width="180" />
      <el-table-column label="名称" align="center" prop="sampleboxName" width="180" />
      <el-table-column label="样本框类型" align="center" prop="sampleboxType" width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.erecord_samplebox_type" :value="scope.row.sampleboxType" />
        </template>
      </el-table-column>
      <el-table-column label="所属区域" align="center" prop="regionName" />
      <el-table-column label="所属街道" align="center" prop="streetName" />
      <el-table-column label="所属社区" align="center" prop="communityName" />
      <el-table-column label="所属街区" align="center" prop="block" />
      <el-table-column label="经度" align="center" prop="longitude" />
      <el-table-column label="纬度" align="center" prop="latitude" />
      <el-table-column label="详细地址" align="center" prop="detailedAddress" width="180" />
      <el-table-column label="参考地址" align="center" prop="referenceAddress" width="180" />
      <el-table-column label="责任单位" align="center" prop="accountabilityUnit" width="180" />
      <el-table-column label="负责企业" align="center" prop="responsibleEnterprise" width="180" />
      <el-table-column label="标段名称" align="center" prop="sectionName" width="180" />
      <el-table-column label="主管单位" align="center" prop="competentOrganization" width="180" />
      <el-table-column label="监管单位" align="center" prop="supervisoryUnit" width="180" />
      <el-table-column label="设施状态" align="center" prop="facilityStatus" />
      <el-table-column label="住宅小区类别" align="center" prop="residentialClass" width="100" />
      <el-table-column label="备注说明" align="center" prop="remark" width="180" />
      <el-table-column label="所在行业" align="center" prop="industry" />
      <el-table-column label="维护开始时间" align="center" prop="maintenanceStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.maintenanceStartTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="维护结束时间" align="center" prop="maintenanceEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.maintenanceEndTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="负责人" align="center" prop="principal" />
      <el-table-column label="片区主管" align="center" prop="districtSupervisor" />
      <el-table-column label="城市管家负责人" align="center" prop="cityPrincipalName" width="140" />
      <el-table-column label="城市管家负责人手机号码" align="center" prop="cityPrincipalPhone" width="180" />
      <el-table-column label="城市管家" align="center" prop="cityStewardName" />
      <el-table-column label="城市管家手机号码" align="center" prop="cityStewardPhone" width="140" />
      <!--
      <el-table-column label="所属等级" align="center" prop="grade" />
      <el-table-column label="要求" align="center" prop="askFor">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.erecord_samplebox_require_type" :value="scope.row.askFor" />
        </template>
      </el-table-column>
      <el-table-column label="巡检地点范围" align="center" prop="inspectionSiteArea" width="180"/>
      -->
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新人" align="center" prop="updateBy" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['erecord:samplebox:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['erecord:samplebox:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改样本框对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="170px" :disabled="isDetail">
        <el-row>
          <el-table-column align="center" label="序号" width="55">
            <template slot-scope="scope">
              <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
            </template>
          </el-table-column>
          <el-col :span="12">
            <el-form-item label="样本框编码" prop="sampleboxCode">
              <el-input v-model="form.sampleboxCode" placeholder="请输入样本框编码" :readonly="!isAdd" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="样本框类型" prop="sampleboxType">
              <el-select v-model="form.sampleboxType" placeholder="请选择样本框类型">
                <el-option v-for="dict in dict.type.erecord_samplebox_type" :key="dict.value" :label="dict.label"
                  :value="parseInt(dict.value)"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="名称" prop="sampleboxName">
              <el-input v-model="form.sampleboxName" placeholder="请输入名称" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="所属区域" prop="region">
              <treeselect v-model="form.region" :options="regionOptions" :show-count="true" placeholder="请选择所属区域" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属街道" prop="street">
              <treeselect v-model="form.street" :options="regionOptions" :show-count="true" placeholder="请选择所属街道" />
            </el-form-item></el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="所属社区" prop="community">
              <treeselect v-model="form.community" :options="regionOptions" :show-count="true" placeholder="请选择所属社区" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属街区" prop="block">
              <el-input v-model="form.block" placeholder="请输入所属街区" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="经度" prop="longitude">
              <el-input v-model="form.longitude" placeholder="请输入经度" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="纬度" prop="latitude">
              <el-input v-model="form.latitude" placeholder="请输入纬度" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="详细地址" prop="detailedAddress">
              <el-input v-model="form.detailedAddress" placeholder="请输入详细地址" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="参考地址" prop="referenceAddress">
              <el-input v-model="form.referenceAddress" placeholder="请输入参考地址" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="责任单位" prop="accountabilityUnit">
              <el-input v-model="form.accountabilityUnit" placeholder="请输入责任单位" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="负责企业" prop="responsibleEnterprise">
              <el-input v-model="form.responsibleEnterprise" placeholder="请输入负责企业" />
            </el-form-item></el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="标段名称" prop="sectionName">
              <el-input v-model="form.sectionName" placeholder="请输入标段名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设施状态" prop="facilityStatus">
              <el-input v-model="form.facilityStatus" placeholder="请输入设施状态" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="主管单位" prop="competentOrganization">
              <el-input v-model="form.competentOrganization" placeholder="请输入主管单位" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="监管单位" prop="supervisoryUnit">
              <el-input v-model="form.supervisoryUnit" placeholder="请输入监管单位" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="所在行业" prop="industry">
              <el-input v-model="form.industry" placeholder="请输入所在行业" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="住宅小区类别" prop="residentialClass">
              <el-input v-model="form.residentialClass" placeholder="请输入住宅小区类别" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="维护开始时间" prop="maintenanceStartTime">
              <el-date-picker clearable v-model="form.maintenanceStartTime" type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择维护开始时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="维护结束时间" prop="maintenanceEndTime">
              <el-date-picker clearable v-model="form.maintenanceEndTime" type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择维护结束时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="负责人" prop="principal">
              <el-input v-model="form.principal" placeholder="请输入负责人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="片区主管" prop="districtSupervisor">
              <el-input v-model="form.districtSupervisor" placeholder="请输入片区主管" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="城市管家负责人" prop="cityPrincipal">
              <el-autocomplete class="inline-input" v-model="form.cityPrincipalName" :fetch-suggestions="getEmpUser"
                placeholder="请输入城市管家负责人" @select="handleCityPrincipalSelect" :debounce="0">
              </el-autocomplete>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="城市管家负责人手机号码" prop="cityPrincipalPhone">
              <el-input v-model="form.cityPrincipalPhone" placeholder="自动填充" readonly />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="城市管家" prop="citySteward">
              <el-autocomplete class="inline-input" v-model="form.cityStewardName" :fetch-suggestions="getEmpUser"
                placeholder="请输入城市管家" @select="handleCityStewardSelect" :debounce="0">
              </el-autocomplete>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="城市管家手机号码" prop="cityStewardPhone">
              <el-input v-model="form.cityStewardPhone" placeholder="自动填充" readonly />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="所属等级" prop="grade">
              <el-input v-model="form.grade" placeholder="请输入所属等级" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="要求" prop="askFor">
              <el-select v-model="form.askFor" placeholder="请选择要求">
                <el-option v-for="dict in dict.type.erecord_samplebox_require_type" :key="dict.value" :label="dict.label"
                  :value="parseInt(dict.value)"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="巡检地点范围" prop="inspectionSiteArea">
              <el-input v-model="form.inspectionSiteArea" placeholder="请输入巡检地点范围" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="备注说明" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" v-show="!isDetail">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSamplebox, getSamplebox, delSamplebox, addSamplebox, updateSamplebox } from "@/api/erecord/samplebox";
import { getRegionTree, getEmpUserByName } from "@/api/erecord/common";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Samplebox",
  dicts: ['erecord_samplebox_type', 'erecord_samplebox_require_type'],
  components: { Treeselect },
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      showTis: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 样本框表格数据
      sampleboxList: [],
      regionOptions: [],
      // 弹出层标题
      title: "",
      // 是否新增
      isAdd: true,
      isDetail: false,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sampleboxCode: undefined,
        sampleboxName: undefined,
        sampleboxType: undefined,
        region: undefined,
        street: undefined,
        community: undefined,
        orderByColumn: "update_time,sampleboxCode",
        isAsc: "desc,asc",
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        sampleboxCode: [
          { required: true, message: "样本框编码不能为空", trigger: "blur" }
        ],
        sampleboxName: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        sampleboxType: [
          { required: true, message: "样本框类型不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getRegionTree();
  },
  methods: {
    /** 查询样本框列表 */
    getList() {
      this.loading = true;
      listSamplebox(this.queryParams).then(response => {
        this.sampleboxList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getRegionTree() {
      getRegionTree().then(response => {
        this.regionOptions = [];
        this.regionOptions = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        sampleboxCode: undefined,
        sampleboxName: undefined,
        sampleboxType: undefined,
        region: undefined,
        street: undefined,
        community: undefined,
        block: undefined,
        longitude: undefined,
        latitude: undefined,
        detailedAddress: undefined,
        referenceAddress: undefined,
        accountabilityUnit: undefined,
        responsibleEnterprise: undefined,
        sectionName: undefined,
        competentOrganization: undefined,
        supervisoryUnit: undefined,
        facilityStatus: undefined,
        residentialClass: undefined,
        remark: undefined,
        industry: undefined,
        maintenanceStartTime: undefined,
        maintenanceEndTime: undefined,
        principal: undefined,
        districtSupervisor: undefined,
        cityPrincipal: undefined,
        cityPrincipalPhone: undefined,
        citySteward: undefined,
        cityStewardPhone: undefined,
        grade: undefined,
        askFor: undefined,
        inspectionSiteArea: undefined,
        createTime: undefined,
        createBy: undefined,
        updateTime: undefined,
        updateBy: undefined,
        delFlag: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.showTis = selection.map(item => item.sampleboxCode)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.isAdd = true;
      this.isDetail = false;
      this.title = "添加样本框";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const sampleboxCode = row.id || this.ids
      getSamplebox(sampleboxCode).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.isAdd = false;
        this.isDetail = false;
        this.title = "修改样本框";
      });
    },
    handleDetail(row) {
      this.loading = true;
      this.isAdd = false;
      this.isDetail = true;
      this.reset();
      const employeeCode = row.id || this.ids
      getSamplebox(employeeCode).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "查看样本框";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.isAdd == false) {
            updateSamplebox(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addSamplebox(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const sampleboxCodes = row.id || this.ids;
      this.$modal.confirm('是否确认删除数据').then(() => { //是否确认删除样本框编号为"' + this.showTis + '"的数据项？
        this.loading = true;
        return delSamplebox(sampleboxCodes);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('erecord/samplebox/export', {
        ...this.queryParams
      }, `样本框列表${this.parseTime(new Date().getTime(), '{y}{m}{d}')}.xlsx`)
    },
    getEmpUser(queryString, cb) {
      //this.loading = true;
      getEmpUserByName(queryString).then(response => {
        this.userNameOptions = [...response.data];
        // this.loading = false;
        console.log("------- user name list : ", this.userNameOptions)
        const result = this.userNameOptions.map(x => ({ ...x, value: x.employeeName }))
        if (result.length == '0') {
          result.push({
            employeeCode: '-1',
            value: '无匹配结果'
          })
        }
        cb(result)
      });
    },
    handleCityPrincipalSelect(item) {
      console.log('handleCityPrincipalSelect -- ', item)
      if (item.employeeCode === '-1') return;
      this.form.cityPrincipal = item.id;
      this.form.cityPrincipalName = item.employeeName;
      this.form.cityPrincipalPhone = item.phone;
    },
    handleCityStewardSelect(item) {
      console.log('handleCityStewardSelect -- ', item)
      if (item.employeeCode === '-1') return;
      this.form.citySteward = item.id;
      this.form.cityStewardName = item.employeeName;
      this.form.cityStewardPhone = item.phone;
    }
  }
};
</script>
